library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  3.1.1     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks plotly::filter(), stats::filter()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
covid = read.csv(file = 'csv/seconddataset.csv')

#Sostituisco il valore di string a date
head(covid)
##         Date     Country Confirmed Deaths Recovered Active New.Cases
## 1 2020-01-23 Afghanistan         0      0         0      0         0
## 2 2020-01-24 Afghanistan         0      0         0      0         0
## 3 2020-01-25 Afghanistan         0      0         0      0         0
## 4 2020-01-26 Afghanistan         0      0         0      0         0
## 5 2020-01-27 Afghanistan         0      0         0      0         0
## 6 2020-01-28 Afghanistan         0      0         0      0         0
##   New.Recovered New.Deaths
## 1             0          0
## 2             0          0
## 3             0          0
## 4             0          0
## 5             0          0
## 6             0          0
dim(covid)
## [1] 101985      9
covid$Date = ymd(covid$Date)
head(covid)
##         Date     Country Confirmed Deaths Recovered Active New.Cases
## 1 2020-01-23 Afghanistan         0      0         0      0         0
## 2 2020-01-24 Afghanistan         0      0         0      0         0
## 3 2020-01-25 Afghanistan         0      0         0      0         0
## 4 2020-01-26 Afghanistan         0      0         0      0         0
## 5 2020-01-27 Afghanistan         0      0         0      0         0
## 6 2020-01-28 Afghanistan         0      0         0      0         0
##   New.Recovered New.Deaths
## 1             0          0
## 2             0          0
## 3             0          0
## 4             0          0
## 5             0          0
## 6             0          0
#Mettono in ordine crescente le date
covid = arrange(covid, Date)
tail(covid)
##              Date            Country Confirmed Deaths Recovered Active
## 101980 2021-06-28          Venezuela    269635   3068    250528  16039
## 101981 2021-06-28            Vietnam     16136     78      6519   9539
## 101982 2021-06-28 West Bank and Gaza    313851   3561    307892   2398
## 101983 2021-06-28              Yemen      6909   1361      4032   1516
## 101984 2021-06-28             Zambia    149661   2091    126441  21129
## 101985 2021-06-28           Zimbabwe     47284   1749     37949   7586
##        New.Cases New.Recovered New.Deaths
## 101980         0             0          0
## 101981       396           200          2
## 101982       130           274          2
## 101983         1             7          1
## 101984      1093          1859         69
## 101985       842           132         13
dw = read.csv(file = 'csv/firstdataset.csv')
head(dw)
##         Date Confirmed Deaths Recovered Active New.Cases Deaths...100.Cases
## 1 2020-01-23       655     18        32    605        99               2.75
## 2 2020-01-24       941     26        39    876       287               2.76
## 3 2020-01-25      1433     42        42   1349       494               2.93
## 4 2020-01-26      2118     56        56   2006       685               2.64
## 5 2020-01-27      2927     82        65   2780       809               2.80
## 6 2020-01-28      5578    131       108   5339      2653               2.35
##   Recovered...100.Cases Deaths...100.Recovered No..of.Countries
## 1                  4.89                  56.25                8
## 2                  4.14                  66.67                9
## 3                  2.93                 100.00               11
## 4                  2.64                 100.00               13
## 5                  2.22                 126.15               16
## 6                  1.94                 121.30               16
dw$Date = ymd(dw$Date)
dw = arrange(dw, Date)
head(dw)
##         Date Confirmed Deaths Recovered Active New.Cases Deaths...100.Cases
## 1 2020-01-23       655     18        32    605        99               2.75
## 2 2020-01-24       941     26        39    876       287               2.76
## 3 2020-01-25      1433     42        42   1349       494               2.93
## 4 2020-01-26      2118     56        56   2006       685               2.64
## 5 2020-01-27      2927     82        65   2780       809               2.80
## 6 2020-01-28      5578    131       108   5339      2653               2.35
##   Recovered...100.Cases Deaths...100.Recovered No..of.Countries
## 1                  4.89                  56.25                8
## 2                  4.14                  66.67                9
## 3                  2.93                 100.00               11
## 4                  2.64                 100.00               13
## 5                  2.22                 126.15               16
## 6                  1.94                 121.30               16
#casi confermati
cnf = '#0066FF'
#casi di morte
dth = '#c00000'
#casi di guarigione
rec = '#00FF66'

fg = plot_ly(dw, x = ~Date)

cnf.l = list(color = cnf, width = 3)
dth.l = list(color = dth, width = 3)
rec.l = list(color = rec, width = 3)

fg = fg %>% add_trace(y = ~Confirmed, name = 'Confermati', mode = 'lines', type ='scatter', line = cnf.l)
fg = fg %>% add_trace(y = ~Recovered, name = 'Guariti', mode = 'lines', type ='scatter', line = rec.l)
fg = fg %>% add_trace(y = ~Deaths, name = 'Morti', mode = 'lines', type ='scatter', line = dth.l)

fg = fg %>% layout(title = ' Casi totali nel mondo di Covid19',
                   xaxis = list(title = 'Tempo'),
                   yaxis = list(title = ' Casi Totali'))
fg
head(covid)
##         Date             Country Confirmed Deaths Recovered Active New.Cases
## 1 2020-01-23         Afghanistan         0      0         0      0         0
## 2 2020-01-23             Albania         0      0         0      0         0
## 3 2020-01-23             Algeria         0      0         0      0         0
## 4 2020-01-23             Andorra         0      0         0      0         0
## 5 2020-01-23              Angola         0      0         0      0         0
## 6 2020-01-23 Antigua and Barbuda         0      0         0      0         0
##   New.Recovered New.Deaths
## 1             0          0
## 2             0          0
## 3             0          0
## 4             0          0
## 5             0          0
## 6             0          0
tail(covid)
##              Date            Country Confirmed Deaths Recovered Active
## 101980 2021-06-28          Venezuela    269635   3068    250528  16039
## 101981 2021-06-28            Vietnam     16136     78      6519   9539
## 101982 2021-06-28 West Bank and Gaza    313851   3561    307892   2398
## 101983 2021-06-28              Yemen      6909   1361      4032   1516
## 101984 2021-06-28             Zambia    149661   2091    126441  21129
## 101985 2021-06-28           Zimbabwe     47284   1749     37949   7586
##        New.Cases New.Recovered New.Deaths
## 101980         0             0          0
## 101981       396           200          2
## 101982       130           274          2
## 101983         1             7          1
## 101984      1093          1859         69
## 101985       842           132         13
latest = covid %>% filter(Date == max(Date)) %>% arrange(desc(Confirmed))

top10 = latest %>% slice(1:10)

top10
##          Date        Country Confirmed Deaths Recovered   Active New.Cases
## 1  2021-06-28             US  33640502 604115         0 33036387     15083
## 2  2021-06-28          India  30316897 397637  29366601   552659     37566
## 3  2021-06-28         Brazil  18448402 514092  16240380  1693930     27804
## 4  2021-06-28         France   5832490 111174    404666  5316650       518
## 5  2021-06-28         Turkey   5414310  49634   5280558    84118      5283
## 6  2021-06-28         Russia   5408744 131671   4911752   365321     21258
## 7  2021-06-28 United Kingdom   4771367 128367     15729  4627271     22723
## 8  2021-06-28      Argentina   4423636  93142   4046308   284186     18389
## 9  2021-06-28          Italy   4258456 127500   4076274    54682       387
## 10 2021-06-28       Colombia   4187194 105326   3880261   201607     28478
##    New.Recovered New.Deaths
## 1              0        150
## 2          56994        907
## 3          79554        618
## 4            271         44
## 5           5327         58
## 6          12594        601
## 7              7          3
## 8          18798        574
## 9           2839         28
## 10         25804        648
summary(top10)
##       Date              Country            Confirmed            Deaths      
##  Min.   :2021-06-28   Length:10          Min.   : 4187194   Min.   : 49634  
##  1st Qu.:2021-06-28   Class :character   1st Qu.: 4510569   1st Qu.:106788  
##  Median :2021-06-28   Mode  :character   Median : 5411527   Median :127934  
##  Mean   :2021-06-28                      Mean   :11670200   Mean   :226266  
##  3rd Qu.:2021-06-28                      3rd Qu.:15294424   3rd Qu.:331146  
##  Max.   :2021-06-28                      Max.   :33640502   Max.   :604115  
##    Recovered            Active           New.Cases     New.Recovered  
##  Min.   :       0   Min.   :   54682   Min.   :  387   Min.   :    0  
##  1st Qu.: 1273565   1st Qu.:  222252   1st Qu.: 7733   1st Qu.:  913  
##  Median : 4061291   Median :  458990   Median :19824   Median : 8960  
##  Mean   : 6822253   Mean   : 4621681   Mean   :17749   Mean   :20219  
##  3rd Qu.: 5188356   3rd Qu.: 3893936   3rd Qu.:26534   3rd Qu.:24052  
##  Max.   :29366601   Max.   :33036387   Max.   :37566   Max.   :79554  
##    New.Deaths   
##  Min.   :  3.0  
##  1st Qu.: 47.5  
##  Median :362.0  
##  Mean   :363.1  
##  3rd Qu.:613.8  
##  Max.   :907.0
top10$Country = factor(top10$Country, levels = c(as.character(top10$Country)))

values = as.character(top10$Confirmed) 

ly1 = plot_ly(top10, x = ~Country, y = ~Confirmed, type = 'bar', name = 'Casi Confermati', text = values, textposition = 'auto', marker = list(color = heat.colors(n=10)))

ly1 = ly1 %>% layout(title = 'Top 10 Nazioni con più casi confermati di Covid19',
                   xaxis = list(title = 'Nazioni'),
                   yaxis = list(title = ' Casi Confermati'))
ly1
fg1 = plot_ly(dw, x = ~Date, y = ~New.Cases, type = 'bar', name = 'Nuovi Casi')
fg2 = plot_ly(dw, x = ~Date, y = ~Deaths...100.Cases, type = 'bar', name = 'Morti / 100 Casi')
fg3 = plot_ly(dw, x = ~Date, y = ~Recovered...100.Cases, type = 'bar', name = 'Guariti / 100 Casi')

subplot(fg1, fg2, fg3, nrows = 3, shareX = FALSE)
ly2 = plot_ly(data = top10, x = ~Confirmed, y = ~Deaths, type = 'scatter', text = values, mode = 'markers', color = ~Country,
       colors = heat.colors(n=10), size = ~Confirmed, marker = list(size = ~1e-4*Deaths))

ly2 = ly2 %>% layout(title = 'Top 10 Nazioni con più morti su casi confermati di Covid19',
                   xaxis = list(title = 'Casi Confermati'),
                   yaxis = list(title = 'Morti'))
ly2
#Italia Analisi
it = covid %>% filter(Country== 'Italy') %>% arrange(Date)

fg1 = plot_ly(it, x = ~Date, y = ~Confirmed, type = 'scatter', mode = 'lines', name = 'Casi Confermati')
fg2 = plot_ly(it, x = ~Date, y = ~Recovered, type = 'scatter', mode = 'lines', name = 'Casi Guariti')
fg3 = plot_ly(it, x = ~Date, y = ~Deaths, type = 'scatter', mode = 'lines', name = 'Casi Morti')

subplot(fg1, fg2, fg3, nrows = 3, shareX = FALSE)
italy = read.csv(file = 'csv/itadataset.csv')
head(italy)
##   SNo                Date Country RegionCode            RegionName Latitude
## 1   0 2020-02-24T18:00:00     ITA         13               Abruzzo 42.35122
## 2   1 2020-02-24T18:00:00     ITA         17            Basilicata 40.63947
## 3   2 2020-02-24T18:00:00     ITA         18              Calabria 38.90598
## 4   3 2020-02-24T18:00:00     ITA         15              Campania 40.83957
## 5   4 2020-02-24T18:00:00     ITA          8        Emilia-Romagna 44.49437
## 6   5 2020-02-24T18:00:00     ITA          6 Friuli Venezia Giulia 45.64944
##   Longitude HospitalizedPatients IntensiveCarePatients
## 1  13.39844                    0                     0
## 2  15.80515                    0                     0
## 3  16.59440                    0                     0
## 4  14.25085                    0                     0
## 5  11.34172                   10                     2
## 6  13.76814                    0                     0
##   TotalHospitalizedPatients HomeConfinement CurrentPositiveCases
## 1                         0               0                    0
## 2                         0               0                    0
## 3                         0               0                    0
## 4                         0               0                    0
## 5                        12               6                   18
## 6                         0               0                    0
##   NewPositiveCases Recovered Deaths TotalPositiveCases TestsPerformed
## 1                0         0      0                  0             NA
## 2                0         0      0                  0             NA
## 3                0         0      0                  0             NA
## 4                0         0      0                  0             NA
## 5               18         0      0                 18             NA
## 6                0         0      0                  0             NA
ly3 = plot_ly(data = italy, x = ~TotalHospitalizedPatients, y = ~IntensiveCarePatients, type = 'scatter', color = ~RegionName)

ly3 = ly3 %>% layout(title = 'Rapporto tra i pazienti totali in ospedale e i casi in terapia intensiva nelle regioni',
                   xaxis = list(title = 'Tot Pazienti ospedale'),
                   yaxis = list(title = 'Pazienti in terapia intensiva'))
ly3
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
l2 = italy %>% filter(Date == max(Date)) %>% arrange(desc(Recovered))

reg10 = l2 %>% slice(1:10)

reg10$RegionName = factor(reg10$RegionName, levels = c(as.character(reg10$RegionName)))

v2 = as.character(reg10$Recovered) 

ly4 = plot_ly(reg10, x = ~RegionName, y = ~Recovered, type = 'bar', text = v2, textposition = 'auto', marker = list(color='orange'))
ly4 = ly4 %>% layout(title = 'Top 10 Regioni Italiane con più casi guariti',
                   xaxis = list(title = 'Regioni'),
                   yaxis = list(title = 'Guariti'))
ly4
l1 = italy %>% filter(Date == max(Date)) %>% arrange(desc(Deaths))

rg10 = l1 %>% slice(1:10)

rg10$RegionName = factor(rg10$RegionName, levels = c(as.character(rg10$RegionName)))

v = as.character(rg10$Deaths) 

ly4 = plot_ly(rg10, x = ~RegionName, y = ~Deaths, type = 'bar', text = v, textposition = 'auto', marker = list(color='orange'))
ly4 = ly4 %>% layout(title = 'Top 10 Regioni Italiane con più morti',
                   xaxis = list(title = 'Regioni'),
                   yaxis = list(title = 'Morti'))
ly4